package cn.com.twoke.easyproject.modules.auth.controller;


import cn.com.twoke.easyproject.common.response.R;
import cn.com.twoke.easyproject.common.response.Result;
import cn.com.twoke.easyproject.modules.auth.request.LoginRequest;
import cn.com.twoke.easyproject.modules.auth.service.LoginService;
import cn.com.twoke.easyproject.modules.auth.service.LogoutService;
import cn.com.twoke.easyproject.modules.auth.service.UserInfoService;
import cn.com.twoke.easyproject.modules.auth.spi.pojo.LoginUserInfo;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 认证接口
 */
@RequestMapping("auth")
@RestController
@Tag(name = "登录认证", description = "用户登录、登出，获取用户信息相关操作")
@RequiredArgsConstructor
public class AuthController {

    private final LoginService loginService;
    private final LogoutService logoutService;
    private final UserInfoService userInfoService;

    /**
     * 用户登录
     * @param request
     * @return
     */
    @PostMapping("login")
    @Operation(summary = "用户登录", description = "通过用户名和密码登录")
    public Result<String> login(@RequestBody @Validated LoginRequest request) {
        return R.success(loginService.loginByUserNameAndPassword(request));
    }

    /**
     * 用户登出
     * @return
     */
    @PostMapping("logout")
    @Operation(summary = "用户登出", description = "用户执行登出操作，访问toke过期")
    public Result logout() {
        logoutService.logout();
        return R.success();
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("info")
    @Operation(summary = "获取登录用户信息", description = "获取当前登录用户的基础信息，包含但不限于用户名、昵称、头像等")
    public Result<LoginUserInfo> getLoginInfo() {
        return R.success(userInfoService.getCurrentUserInfo());
    }

}
